package com.kexuexiong.demo.bear.common.utils;

import org.apache.logging.log4j.util.Strings;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

public class BearMultipartFile implements MultipartFile {
    private final String name;
    private String originalFilename;
    @Nullable
    private final String contentType;
    private final byte[] content;

    public BearMultipartFile(String name, @Nullable byte[] content) {
        this(name, "", (String) null, (byte[]) content);
    }

    public BearMultipartFile(String name, InputStream contentStream) throws IOException {
        this(name, "", (String) null, (byte[]) FileCopyUtils.copyToByteArray(contentStream));
    }

    public BearMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType, @Nullable byte[] content) {
        Assert.hasLength(name, "Name must not be empty");
        this.name = name;
        this.originalFilename = originalFilename != null ? originalFilename : "";
        this.contentType = contentType;
        this.content = content != null ? content : new byte[0];
    }

    public BearMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType, @Nullable String fileUrl) throws Exception {
        Assert.hasLength(name, "Name must not be empty");
        this.name = name;
        this.originalFilename = originalFilename != null ? originalFilename : "";
        this.contentType = contentType;
        File file = ResourceUtils.getFile(fileUrl);
        long length = file.length();
        if (length > 1024 * 1024 * 500) {
            throw new RuntimeException("文件不能大于500m!");
        }
        if (Strings.isBlank(this.originalFilename)) {
            this.originalFilename = file.getName();
        }
        byte[] fileByte = new byte[(int) length];
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            fileInputStream.read(fileByte);
        } catch (Exception e) {
            throw new RuntimeException("获取出错!", e);
        }
        this.content = fileByte;
    }

    public BearMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType, InputStream contentStream) throws IOException {
        this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
    }

    public String getName() {
        return this.name;
    }

    @NonNull
    public String getOriginalFilename() {
        return this.originalFilename;
    }

    @Nullable
    public String getContentType() {
        return this.contentType;
    }

    public boolean isEmpty() {
        return this.content.length == 0;
    }

    public long getSize() {
        return (long) this.content.length;
    }

    public byte[] getBytes() throws IOException {
        return this.content;
    }

    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(this.content);
    }

    public void transferTo(File dest) throws IOException, IllegalStateException {
        FileCopyUtils.copy(this.content, dest);
    }
}
